#include "allhead.h"
#include "Graf.h"

#define printf0  if( 0 )printf
#define printf1  if( 0 )printf
#define pr0			 0

#define n1 60 //63
#define n2 60 //63
static  CACH c[n1][n2];

#define c_ind(a,n) (  ( (int) ((a/2+1)*(float)(n-1)) ) % (n)  )

int	SetVertexFunc(GRAF graf_base, LPVERTEX pv)
{
 int ret,i1,i2,i3;

 ret=FindCoverTrg(graf_base,*pv,&i1,&i2,&i3);

 if(ret) printf("SetVertexFunc: unchecked point\n");

// if(i1==i2 && i1==i3) printf(".");

 if(!ret)	ApprocZ(pv,graf_base.vertex[i1],graf_base.vertex[i2],graf_base.vertex[i3]);
 else		pv->z=0.f;

 return ret;
}

//--------------------------------------------

int FindCoverTrg(GRAF graf, VERTEX v, LPINT pi1, LPINT pi2, LPINT pi3)
{
 int i;

 for(i=0;i<graf.nt;i++)
	{
	*pi1=graf.trg[3*i+0];
	*pi2=graf.trg[3*i+1];
	*pi3=graf.trg[3*i+2];

	if	(
		is_trg_cover(
			v,
			graf.vertex[*pi1],
			graf.vertex[*pi2],
			graf.vertex[*pi3]
			)
		)
		return 0;
	}

 return 1;
}

//--------------------------------------------


void ApprocZ(LPVERTEX pv, VERTEX v1, VERTEX v2, VERTEX v3)
{
#if 0
// pv->z=v1.z;
   pv->z=(v1.z+v2.z+v3.z)/3.f;

 return;
#else
 double a=0.,b=0.,r21y,r21z,r31y,r31z,ry,rz,r21x,r31x,rx,d;
 static int prn=0;//12345;

 r21x=v2.x-v1.x;
 r31x=v3.x-v1.x;

 r21y=v2.y-v1.y;
 r31y=v3.y-v1.y;

 r21z=v2.z-v1.z;
 r31z=v3.z-v1.z;

 rx=pv->x-v1.x;
 ry=pv->y-v1.y;

 d=r31x*r21y-r31y*r21x;

 if(fabs(d)>EPS)
	{
	b= (  rx*r21y-  ry*r21x)/d;
	a= (r31x*ry  -r31y*  rx)/d;
 
	rz=a*r21z+b*r31z;
	}
 else
	rz=0.;

 if(prn)
	{
	 if(prn==12345)printf("\n ??\n");
	printf("a=%15g b=%15g rz=%15g prn=%d input -1/0/1\n",a,b,rz,prn);
	scanf("%d",&prn);
//	printf("prn=%d\n",prn);
	if(prn<0)exit(1);
	}

 pv->z=(float)(rz+v1.z);
#endif
}

int is_trg_cover(VERTEX v, VERTEX v1, VERTEX v2, VERTEX v3)
{
 double s,s1,s2,s3,eps=EPS;

 s=strg(v1,v2,v3);

 if(s>0)
	{
	 s1=strg(v,v2,v3);
	 s2=strg(v1,v,v3);
	 s3=strg(v1,v2,v);

	 if(fabs(s1+s2+s3-s)<s*eps)
		 return 1;
	}
/*
 else
	{
	 if	(
		 fabs(lrib(v,v1)+lrib(v,v2)-lrib(v1,v2))<lrib(v1,v2)*eps ||
		 fabs(lrib(v,v2)+lrib(v,v3)-lrib(v2,v3))<lrib(v2,v3)*eps ||
		 fabs(lrib(v,v3)+lrib(v,v1)-lrib(v3,v1))<lrib(v3,v1)*eps
		)
		 return 1;
	}
*/
 return 0;
}

double strg(VERTEX v1,VERTEX v2,VERTEX v3)
{
 double s;

 s=  v1.x*v2.y+v2.x*v3.y+v3.x*v1.y
	 -
	(v2.x*v1.y+v3.x*v2.y+v1.x*v3.y);

 s=fabs(s);

 return s;
}

double strg3(VERTEX v1,VERTEX v2,VERTEX v3)
{
 double s;
 double ax,ay,az,bx,by,bz;

 ax=v1.x-v3.x;
 ay=v1.y-v3.y;
 az=v1.z-v3.z;
 bx=v2.x-v3.x;
 by=v2.y-v3.y;
 bz=v2.z-v3.z;
 
 s=sqrt( qva(ay*bz-az*by) + qva(ax*bz-az*bx) + qva(ax*by-ay*bx) );

 return s;
}

char settrgori(VERTEX v1,VERTEX v2,VERTEX v3)
{
 char ori;
 double ax,ay,az,bx,by,bz,g;

 ax=v1.x-v3.x;
 ay=v1.y-v3.y;
 az=v1.z-v3.z;
 bx=v2.x-v3.x;
 by=v2.y-v3.y;
 bz=v2.z-v3.z;

 ori=0;
 g=ay*bz-az*by;			 
 ori |= ((g<-EPSO)* 1);
 ori |= ((g> EPSO)* 2);

 g=-ax*bz+az*bx;
 ori |= ((g<-EPSO)* 4);
 ori |= ((g> EPSO)* 8);

 g=ax*by-ay*bx;			// if (|g|<EPS0) then alf ( (v1,v2,v3), (x,y) ) = 90
 ori |= ((g<-EPSO)*16);
 ori |= ((g> EPSO)*32);

// printf("%1x%1x%1x\n",(ori&3),((ori/4)&3),((ori/16)&3));

 return ori;
}

double sign_strg(VERTEX v1,VERTEX v2,VERTEX v3)
{
 double s;

 s=  v1.x*v2.y+v2.x*v3.y+v3.x*v1.y
	 -
	(v2.x*v1.y+v3.x*v2.y+v1.x*v3.y);

 return s;
}

double lrib(VERTEX v1, VERTEX v2)
{
 double r;

 r=sqrt(qva(v1.x-v2.x)+qva(v1.y-v2.y)+qva(v1.z-v2.z));

 return r;
}


//++++++++++++++++++++++++++++++++++++++=
